package com.epfresh.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.epfresh.R;
import com.epfresh.adapter.MyPagerAdapter;
import com.epfresh.fragment.SearchGoodsFragment;
import com.epfresh.fragment.SearchStoreFragment;
import com.epfresh.global.BaseActivity;
import com.epfresh.utils.TabUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class SearchResultActivity extends BaseActivity  implements TabLayout.OnTabSelectedListener{
    private TabLayout tabLayout ;
    private ViewPager viewPager;
    private MyPagerAdapter myPagerAdapter;
    private List<String> datas = new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    private String keyWords;
    private String marketId;
    private String store;
    private SearchGoodsFragment searchGoodsFragment=new SearchGoodsFragment();
    private SearchStoreFragment searchStoreFragment=new SearchStoreFragment();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        getIntentData();
        initView();
        initData();
    }

    @Override
    protected View getToolBar() {
        View actionbar = getToolBarView();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, getResources().getDimensionPixelSize(com.epfresh.api.R.dimen.tool_bar_height));
        actionbar.setLayoutParams(lp);
        this.toolbarTitle = actionbar.findViewById(R.id.toolbar_title);
        return actionbar;
    }

    private void initView(){
        toolbarTitle.setText("搜索结果");
        tabLayout=findViewById(R.id.tab_layout);
        viewPager=findViewById(R.id.view_pager);
    }

    private void getIntentData() {
        keyWords = getIntent().getStringExtra("title");
        store=getIntent().getStringExtra("store");
        if (getIntent().getStringExtra("marketId") != null) {
            marketId = getIntent().getStringExtra("marketId");
        }
    }


    private void initData(){
        datas.add("商品");
        datas.add("商户");
        //循环注入标签
        for (String tab : datas) {
            tabLayout.addTab(tabLayout.newTab().setText(tab));
        }
        searchGoodsFragment.setData(keyWords,marketId);
        searchStoreFragment.setData(keyWords,marketId);
        fragments.add(searchGoodsFragment);
        fragments.add(searchStoreFragment);
        tabLayout.addOnTabSelectedListener(this);
        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), datas, fragments);
        viewPager.setAdapter(myPagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
        //设置分割线
        LinearLayout linearLayout = (LinearLayout) tabLayout.getChildAt(0);
        linearLayout.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
        linearLayout.setDividerDrawable(ContextCompat.getDrawable(this,R.drawable.divider_vertical));
        linearLayout.setDividerPadding(30);

        TabUtils.reflex(tabLayout,60);
        if(!TextUtils.isEmpty(store)&& store.equals("store")){
            viewPager.setCurrentItem(1);
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

}
